package dbms;

import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DBAddingFunctions {

	/*
	 * PROGRAMS CONFIGURATION PARAMETERS:
	 * - type: EXECUTABLE or JAVA
	 * - executingName
	 * - dir
	 * - extenzion
	 */

	public static boolean setProgramParameter(String programName, int parameter, String value)
	{
		try {
			DBConnection.getIstance().setAutoCommit(false);
			
			String sql;
			if(!DBCheckAndConfigurationFunctions.isProgramConfigured(programName))
				sql = "INSERT INTO " + DBTables.CONFIG + "(program, parameter, value) VALUES (?, ?, ?)";
			else
				sql = "UPDATE " + DBTables.CONFIG + " SET value=? WHERE program=? AND parameter=?";
				
			PreparedStatement ps = DBConnection.getIstance().prepareStatement(sql);
			
			if(!DBCheckAndConfigurationFunctions.isProgramConfigured(programName))
			{
				ps.setString(1, programName);
				ps.setInt(2, parameter);
				ps.setString(3, value);
			} else {
				ps.setString(1, value);
				ps.setString(2, programName);
				ps.setInt(3, parameter);	
			}
			
			ps.executeUpdate();
			
			DBConnection.getIstance().commit();
			DBConnection.getIstance().setAutoCommit(true);
			
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	/*
	public static boolean setProgramExecutingName(String programName, String executingName)
	{
		try {
			DBConnection.getIstance().setAutoCommit(false);

			String sql;
			if(!DBCheckAndConfigurationFunctions.isProgramConfigured(programName))
				sql = "INSERT INTO " + DBTables.CONFIG + "(program, parameter, value) VALUES (?, ?, ?)";
			else
				sql = "UPDATE " + DBTables.CONFIG + " SET value=? WHERE program=? AND parameter=?";
			
			PreparedStatement ps = DBConnection.getIstance().prepareStatement(sql);
			
			if(!DBCheckAndConfigurationFunctions.isProgramConfigured(programName))
			{
				ps.setString(1, programName);
				ps.setInt(2, DBParameters.PROGRAMEXECUTINGNAME);
				ps.setString(3, executingName);
			} else {
				ps.setString(1, executingName);
				ps.setString(2, programName);
				ps.setInt(3, DBParameters.PROGRAMEXECUTINGNAME);	
			}
			
			ps.executeUpdate();
			
			DBConnection.getIstance().commit();
			DBConnection.getIstance().setAutoCommit(true);
			
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean setProgramDir(String programName, String dir)
	{
		try {
			DBConnection.getIstance().setAutoCommit(false);
			
			String sql;
			if(!DBCheckAndConfigurationFunctions.isProgramConfigured(programName))
				sql = "INSERT INTO " + DBTables.CONFIG + "(program, parameter, value) VALUES (?, ?, ?)";
			else
				sql = "UPDATE " + DBTables.CONFIG + " SET value=? WHERE program=? AND parameter=?";

			PreparedStatement ps = DBConnection.getIstance().prepareStatement(sql);
			
			if(!DBCheckAndConfigurationFunctions.isProgramConfigured(programName))
			{
				ps.setString(1, programName);
				ps.setInt(2, DBParameters.PROGRAMDIR);
				ps.setString(3, dir);
			} else {
				ps.setString(1, dir);
				ps.setString(2, programName);
				ps.setInt(3, DBParameters.PROGRAMDIR);	
			}
			
			ps.executeUpdate();
			
			DBConnection.getIstance().commit();
			DBConnection.getIstance().setAutoCommit(true);
			
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	*/
}
